namespace Digitalmes.Application.Sys.Configs.Commands;

/// <summary>
/// 系统配置项创建命令。
/// </summary>
/// <param name="Config"></param>
public record CreateConfigCommand(SysConfigDto Config) : IRequest<IResult<bool>>;

internal sealed class CreateConfigCommandHandler(IRepository<SysConfig> repo) : IRequestHandler<CreateConfigCommand, IResult<bool>>
{
    public async Task<IResult<bool>> Handle(CreateConfigCommand request, CancellationToken cancellationToken)
    {
        // 检查编号是否已存在。
        if (await repo.IsAnyAsync(m => m.Code == request.Config.Code, cancellationToken))
        {
            return Result.Failure("系统配置编号已存在");
        }

        var config = request.Config.Adapt<SysConfig>();
        var ok = await repo.InsertAsync(config, cancellationToken);
        return Result.From(ok);
    }
}
